/**
 * The details view.
 *
 * (c) Copyright 2011 Nokia Corporation. All rights reserved.
 */

import QtQuick 1.1
import com.nokia.symbian 1.1

import "../script/ajax.js" as AjaxScript
import "../script/common.js" as CommonScript

Page {
    id: detailsView;

    // The ID of the Beta currently on the screen
    property variant betaId;
    // Pre-fetched download information
    property variant downloadInfo;

    tools: ToolBarLayout {
         ToolButton {
             iconSource: "toolbar-back";
             onClicked: {
                 busyIndicator.opacity = 0;
                 busyIndicator.running = false;
                 detailsView.pageStack.pop();
             }
         }
    }

    Flickable {
        id: flickableDetailsView;

        anchors {
            fill: parent;
            leftMargin: 10;
            rightMargin: 10;
        }

        contentWidth: width;
        contentHeight: detailsTopBar.height + betaTitle.height + 5 +
                       betaImg.height + 5 + betaOverview.height;
        flickableDirection: "VerticalFlick";

        // Top bar
        TopBar {
            id: detailsTopBar;
            width: parent.width;
            height: 70;
            titleImage: "qrc:/gfx/title_application_details.png";
        }

        ScrollDecorator {
            flickableItem: flickableDetailsView;
        }

        Text {
            id: errorTextDetailsView;
            anchors.top: detailsTopBar.bottom;
            width: parent.width;
            text: "Unable to fetch details. Ensure that your device is connected to the Internet.";
            opacity: 0;
            wrapMode: "WordWrap";
        }

        Text {
            id: betaTitle;
            x: 0;
            anchors.top: detailsTopBar.bottom;
            width: parent.width;
            font.pointSize: 10;
            horizontalAlignment: "AlignHCenter";
            wrapMode: "WordWrap";
        }

        Image {
            id: betaImg;
            anchors.top: betaTitle.bottom;
            anchors.topMargin: 5;
            width: 150;
            height: 150;
        }

        Button {
            id: btnDownload;
            anchors.top: betaTitle.bottom;
            anchors.left: betaImg.right;
            anchors.topMargin: 5;
            anchors.leftMargin: 20;
            width: parent.width / 2.2;
            text: "Download";
            opacity: 0;

            onClicked: {
                if (!detailsView.pageStack.busy) {
                    showDownloadInfoView();
                }
            }
        }

        Button {
            id: btnViewReviews;
            anchors.top: btnDownload.bottom;
            anchors.left: betaImg.right;
            anchors.topMargin: 5;
            anchors.leftMargin: 20;
            width: parent.width / 2.2;
            text: "View Reviews";
            opacity: 0;

            onClicked: {
                if (!detailsView.pageStack.busy) {
                    showReviewView();
                }
            }
        }

        Text {
            id: betaRating;
            anchors.top: btnViewReviews.bottom;
            anchors.left: betaImg.right;
            anchors.topMargin: 5;
            anchors.leftMargin: 20;
            font.pointSize: 8;
        }

        Text {
            id: betaOverview;
            anchors.top: betaImg.bottom;
            anchors.topMargin: 5;
            width: parent.width;
            wrapMode: "WordWrap";
        }
    }

    function fetchDetails(betaId) {
        var req = AjaxScript.createXMLHttpRequest();

        if (req) {
            var url = CommonScript.BETA_LABS_API_URL + "/application/" + betaId;
            AjaxScript.loadXMLDoc(req, url, cbFetchDetails);
        }
    }

    // Called when the server responds to the fetch details call
    function cbFetchDetails(xml, text) {
        if (busyIndicator) {
            busyIndicator.opacity = 0;
            busyIndicator.running = false;
        }

        if (xml === null) {
            errorTextDetailsView.opacity = 1;
            return;
        }

        errorTextDetailsView.opacity = 0;

        betaId = CommonScript.getElementsByTagName(
                xml.documentElement, "id")[0].firstChild.nodeValue;

        var title = CommonScript.getElementsByTagName(
                xml.documentElement, "title")[0].firstChild.nodeValue;
        betaTitle.text = title;

        var imgSrc = CommonScript.getElementsByTagName(
                xml.documentElement, "image")[0].firstChild.nodeValue;
        betaImg.source = imgSrc;

        btnDownload.opacity = 1;
        btnViewReviews.opacity = 1;

        var rating = CommonScript.getElementsByTagName(
                xml.documentElement, "rating")[0].firstChild.nodeValue;
        betaRating.text = "Rating: " + Math.round(rating);

        var overview = CommonScript.getElementsByTagName(
                xml.documentElement, "overview")[0].firstChild.nodeValue;

        // Remove objects from the HTML code.
        var objectStartInd;
        while ((objectStartInd = overview.indexOf("<object")) != -1) {
            var objectEndInd = overview.indexOf("</object>", objectStartInd)
                + "</object>".length;
            overview = overview.substring(0, objectStartInd)
                    + overview.substring(objectEndInd);
        }

        betaOverview.text = overview;

        // Load the download information into a variable even though the
        // download info view has not been activated yet. This saves bandwidth,
        // because no HTTP request is needed to fetch this same information
        // later.
        var downloadNode = CommonScript.getElementsByTagName(
                xml.documentElement, "download")[0];
        // Download info can be empty. In this case, disable the download button.
        if (downloadNode.firstChild === undefined) {
            btnDownload.enabled = false;
        } else {
            downloadInfo = downloadNode.firstChild.nodeValue;
        }
    }

    function showDownloadInfoView() {
        var downloadInfoPage =
            detailsView.pageStack.push(Qt.resolvedUrl("DownloadInfoView.qml"));
        downloadInfoPage.showDownloadInfo(downloadInfo);
    }

    function showReviewView() {
        var reviewPage =
            detailsView.pageStack.push(Qt.resolvedUrl("ReviewView.qml"));
        reviewPage.showReviews(betaId);
    }
}
